//表单验证
$(function(){
	$('#mobile').blur(function () {
		var mobile = $('#mobile').val();
		var shopId = $('#shopId').val();
		if(mobile == null || mobile == ''){
			layer.msg("请输入您的手机号!",{icon:2,time:2000});
			return;
		}
		if(mobile.length > 11){
			layer.msg("手机号不能超过11位!",{icon:2,time:2000});
			return;
		}
		if(!isPhoneNo(mobile)){
			layer.msg("请输入11位正常手机号!",{icon:2,time:2000});
			return;
		}
		$.ajax({
			url:baselocation+'/user/user-info/'+shopId+'/'+$('#mobile').val(),
			type:'get',
			dataType:'json',
			success:function(result){
				if(result.success==true){
					$("#name").val(result.data.customerName);
					$("#address").val(result.data.address);
					$("#email").val(result.data.email);
					$("#weixin").val(result.data.weixin);
					$("#customerDiscount").val(result.data.customerDiscount);
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	})
	$('#saveUserBtn').click(function () {
		//获取验证器
		var name = $('#name').val();
		var weixin = $('#weixin').val();
		var mobile = $('#mobile').val();
		var email = $('#email').val();
		var address = $('#address').val();
		
		if(mobile == null || mobile == ""){
			layer.msg("请输入您的手机号!",{icon:2,time:2000});
			return;
		}
		if(mobile.length > 11){
			layer.msg("手机号不能超过11位!",{icon:2,time:2000});
			return;
		}
		if(!isPhoneNo(mobile)){
			layer.msg("请输入11位正常手机号!",{icon:2,time:2000});
			return;
		}
		if(name == null || name == ""){
			layer.msg("请输入您的姓名或昵称!",{icon:2,time:2000});
			return;
		}
		if(name.length > 32){
			layer.msg("姓名不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(weixin == null || weixin == ""){
			layer.msg("请输入微信号以便可以及时联系您!",{icon:2,time:2000});
			return;
		}
		if(weixin.length > 32){
			layer.msg("微信号不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(email != null && email.length > 32){
			layer.msg("邮箱不能超过32位!",{icon:2,time:2000});
			return;
		}
		if(address != null && address.length > 512){
			layer.msg("地址不能超过512位!",{icon:2,time:2000});
			return;
		}
		var user = {};
		user["customerName"] = name;
		user["weixin"] = weixin;
		user["loginName"] = mobile;
		user["mobile"] = mobile;
		user["email"] = email;
		user["address"] = address;
		
		var productId = $('#productId').val();
		$.ajax({
			url:baselocation+'/user/user-info',
			contentType:'application/json',
			type:'post',
			dataType:'json',
			data: JSON.stringify(user),
			success:function(result){
				if(result.success==false){
					layer.msg(result.message,{icon:2,time:2000});
				}else{
					$.cookie("gou-loginName", result.data.loginName, { path: "/", expires: 100000});
					$.cookie("gou-name", result.data.customerName, { path: "/", expires: 100000});
					$.cookie("gou-weixin", result.data.weixin, { path: "/", expires: 100000});
					$.cookie("gou-address", result.data.address, { path: "/", expires: 100000});
					$.cookie("gou-email", result.data.email, { path: "/", expires: 100000});
					window.location.href=baselocation+'/order/product/'+productId+'/go-buy';
				}
			},
			error:function(result){
				alert("系统异常,请联系管理员！");
			}
		});
	})
});